﻿#include "buss_main.h"
#include "ui_buss_main.h"
#include "buss_menu.h"
#include "buss_order.h"
#include "buss_comment.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QDebug>
#include <qsqlerror.h>
#include <QCloseEvent>
#include <QScreen>
#include <QGuiApplication>

Buss_main::Buss_main(QWidget *parent) : QWidget(parent),ui(new Ui::Buss_main)
{
    ui->setupUi(this);
    setWindowTitle("商家后台管理系统");    
    // 设置窗口大小
    resize(1250, 800);
    setMinimumSize(1000, 600);
    // 初始化数据库连接
    setupDatabase();
}

void Buss_main::setupDatabase()
{
    db = QSqlDatabase::database();
    if (!db.isOpen()) {
        if (!db.open()) {
            QMessageBox::critical(this, "错误", "无法连接数据库: " + db.lastError().text());
            qDebug() << "数据库连接失败:" << db.lastError().text();
        }
    }
}

void Buss_main::setBusiness1_id(int bus_id){
    m_businessId = bus_id;
    // 查询商家信息并显示
   QSqlQuery query;
   query.prepare("SELECT business_name FROM business WHERE business_id = :id");
   query.bindValue(":id", m_businessId);

   if (query.exec() && query.next()) {
       QString businessName = query.value(0).toString();
       ui->label_welcome->setText(QString("欢迎，%1").arg(businessName));
       qDebug() << "商家名称:" << businessName;
   } else {
       QMessageBox::warning(this, "警告", "未能获取商家信息!");
   }

}
void Buss_main::setBusiness1_name(QString name){
    bus_name = name;
    ui->label_welcome->setText(QString("欢迎，%1").arg(bus_name));
}
Buss_main::~Buss_main()
{
    qDebug() << "Buss_main析构函数被调用，商家ID:" << m_businessId;
    delete ui;
}

void Buss_main::closeEvent(QCloseEvent *event)
{
    qDebug() << "Buss_main窗口即将关闭，商家ID:" << m_businessId;
    emit windowAboutToClose(); // 发送窗口即将关闭的信号
    event->accept(); // 接受关闭事件
}
//跳转登录界面
void Buss_main::on_pushButton_return_clicked()
{    
    qDebug() << "商家后台返回按钮被点击，关闭窗口";
    // 关闭当前窗口
    this->close();
}
//跳转菜单管理
void Buss_main::on_pushButton_menu_clicked()
{
    Buss_menu *b=new Buss_menu(m_businessId);
    b->show();
    this->close(); // 关闭当前窗口而不是隐藏
    return;

}
//跳转订单管理
void Buss_main::on_pushButton_order_clicked()
{
    Buss_order *b=new Buss_order(m_businessId);
    b->show();
    this->close(); // 关闭当前窗口而不是隐藏
    return;
}
//跳转评论管理
void Buss_main::on_pushButton_comment_clicked()
{
    Buss_comment *b = new Buss_comment(m_businessId);
    b->show();
    return;
}
//欢迎商家
void Buss_main::on_label_welcome_linkActivated(const QString &link)
{
    ui->label_welcome->setText(link);
}

